$(function () {
    let materielCode = getQueryString("materielCode");
    let url = '../materiel/list';
    if (materielCode) {
        url += '?materielCode=' + materielCode;
    }
    $("#jqGrid").Grid({
        url: url,
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '物料编码', name: 'materielCode', index: 'materiel_code', width: 120},
            {label: '物料名称', name: 'materielName', index: 'materiel_name', width: 80},
            {label: '物料类型', name: 'materielType', index: 'materiel_type', width: 80},
            {label: '供货渠道', name: 'supplyChannel', index: 'supply_channel', width: 80},
            {label: '品牌', name: 'brand', index: 'brand', width: 80},
            {label: '物料描述', name: 'materielDesc', index: 'materiel_desc', width: 80},
            {
                label: '上架', name: 'isOnSale', index: 'is_on_sale', width: 50,
                formatter: function (value) {
                    return transIsNot(value);
                }
            },
            {
                label: '录入日期', name: 'addTime', index: 'add_time', width: 80, formatter: function (value) {
                    return transDate(value, 'yyyy-MM-dd');
                }
            },
            {
                label: '删除', name: 'isDelete', index: 'is_delete', width: 50,
                formatter: function (value) {
                    return transIsNot(value);
                }
            }]
    });

    new AjaxUpload('#upload', {
        action: '../materiel/import',
        name: 'file',
        autoSubmit: true,
        responseType: "json",
        onSubmit: function (file, extension) {
            if (!(extension && /^(xls|xlsx)$/.test(extension.toLowerCase()))) {
                alert('只支持xls、xlsx格式的文档！');
                return false;
            }
        },
        onComplete: function (file, r) {
            if (r.code == 0) {
                vm.reload();
            } else {
                alert(r.msg);
            }
        }
    });

});

let vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        materiel: {},
        ruleValidate: {
            materielCode: [
                {required: true, message: '物料编码不能为空', trigger: 'blur'}
            ]
        },
        q: {
            materielCode: ''
        },
        materiels: [],
        type: ''
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.materiel = {};
            vm.getMateriels();
            vm.type = 'add';
        },
        update: function (event) {
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";
            vm.type = 'update';

            vm.getInfo(id)
        },
        /*changeMateriels: function (opt) {
            let goodsId = opt.value;
            $.get("../goods/info/" + goodsId, function (r) {
                if (vm.type == 'add') {
                    vm.product.goodsSn = r.goods.goodsSn;
                    vm.product.goodsNumber = r.goods.goodsNumber;
                    vm.product.retailPrice = r.goods.retailPrice;
                    vm.product.marketPrice = r.goods.marketPrice;
                }
                $.get("../goodsspecification/queryAll?goodsId=" + goodsId + "&specificationId=1", function (r) {
                    vm.colors = r.list;
                });
                $.get("../goodsspecification/queryAll?goodsId=" + goodsId + "&specificationId=2", function (r) {
                    vm.guiges = r.list;
                });
                $.get("../goodsspecification/queryAll?goodsId=" + goodsId + "&specificationId=4", function (r) {
                    vm.weights = r.list;
                });
            });
        },*/
        saveOrUpdate: function (event) {
            let url = vm.materiel.id == null ? "../materiel/save" : "../materiel/update";
            $.ajax({
                type: "POST",
                url: url,
                contentType: "application/json",
                data: JSON.stringify(vm.materiel),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }
            confirm('确定要删除选中的记录？', function () {
                $.ajax({
                    type: "POST",
                    url: "../materiel/delete",
                    contentType: "application/json",
                    data: JSON.stringify(ids),
                    success: function (r) {
                        if (r.code == 0) {
                            alert('操作成功', function (index) {
                                $("#jqGrid").trigger("reloadGrid");
                            });
                        } else {
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getInfo: function (id) {
            $.get("../materiel/info/" + id, function (r) {
                vm.materiel = r.materiel;
                vm.getMateriels();
            });
        },
        reload: function (event) {
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'materielCode': vm.q.materielCode},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        handleSubmit: function (materielCode) {
            handleSubmitValidate(this, materielCode, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (materielCode) {
            handleResetForm(this, materielCode);
        },
        getMateriels: function () {
            $.get("../materiel/queryAll/", function (r) {
                vm.materiels = r.list;
            });
        },

        importMateriel: function () {
            importFile('#rrapp', '../materiel/batchImport', {'username': vm.q.username});
        },

        importFile:function(formId, url, extraObj){
            /**
             * 主要功能:导出功能公共方法
             * @param formId 表单ID,带'#'号,如'#formId'
             * @param url 请求后台地址
             * @param extraObj 往后台请求额外参数,对象格式,如:{'aaa': 111}
             */
            var form = $('<form>'); //定义一个form表单
            form.attr('style', 'display: none');
            form.attr('target', '');
            form.attr('method', 'post');
            form.attr('action', url);

            var json = getJson(formId);
            if (typeof extraObj != 'undefined') {
                json = $.extend(json, extraObj);
            }

            $('body').append(form);//将表单放置在web中
            for (var i in json) {
                var input = $('<input>');
                input.attr('type', 'hidden');
                input.attr('name', i);
                input.attr('value', json[i]);
                form.append(input);
            }
            form.submit();//表单提交
        }


    }
});